const consts = {
  sessionKeys: {
    JWT_TOKEN: 'JWT_TOKEN',
    LOGIN_USER_DESC: 'LOGIN_USER_DESC',
    CURRENT_DEPT: 'CURRENT_DEPT'
  },
  exceptions: {
    NUM_LENGTH_TOO_LONG: {
      key: 'NumLengthTooLong',
      msg: '数字太大，超出限制!'
    }
  },
  orgImgBasePath: process.env.VUE_APP_SUPERMALL_ORG_IMG_BASEURL
}
const funs = {
  logout () {
    window.localStorage.setItem(consts.sessionKeys.JWT_TOKEN, '')
    window.localStorage.setItem(consts.sessionKeys.CURRENT_DEPT, '')
    window.localStorage.setItem(consts.sessionKeys.LOGIN_USER_DESC, '')
    window.location.href = '/#/login'
    setTimeout(() => {
      window.location.reload()
    }, 200)
  },
  getObjFromLocalStorage: function (key) {
    let jsonStr = localStorage.getItem(key)
    if (jsonStr != null && jsonStr != '') {
      return JSON.parse(jsonStr)
    } else {
      return null
    }
  },
  loginUserInfo: function () {
    let result = this.getObjFromLocalStorage(consts.sessionKeys.LOGIN_USER_DESC)
    if (result == null || result.roles == null || result.roles.length < 1) {
      funs.logout()
    } else {
      return result
    }
  },
  checkIsLogined () {
    if (!localStorage.getItem(consts.sessionKeys.LOGIN_USER_DESC)) {
      this.logout()
    }
  },
  getCurrentStore: function () {
    funs.checkIsLogined()
    return this.getObjFromLocalStorage(consts.sessionKeys.CURRENT_DEPT)
  },
  getApproveDes (approveStat) {
    if (approveStat == 0) {
      return '未审核'
    } else if (approveStat == 1) {
      return '已审核'
    } else if (approveStat == 2) {
      return '已终止'
    }
  }
}
const utils = {
  isEmpty: function (str) {
    if (str == null) {
      return true
    } else {
      if (str == '') {
        return true
      } else {
        return false
      }
    }
  },
  numberFormat: function (num, tarLen) {
    num = '' + num
    if (tarLen == num.length) {
      return num
    } else if (num.length > tarLen) {
      throw consts.exceptions.NUM_LENGTH_TOO_LONG.key
    } else {
      var fillLen = tarLen - num.length
      for (let i = 0; i < fillLen; i++) {
        num = '0' + num
      }
      return num
    }
  }
}

export default { consts, funs, utils }
